package com.fahai.web.controller;/**
 * Created by Lilinjun on 17/7/26.
 */


import com.fahai.common.PageVo;
import com.fahai.utils.DateUtil;
import com.fahai.web.entity.CaseInvstg;
import com.fahai.web.service.CaseInvstgService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;


/**
 * 案件调查控制器
 *
 * @author
 * @create 2017-07-26 下午5:51
 **/
@Controller
@RequestMapping("/caseinvstg")
public class CaseInvstgController {

    @Autowired
    CaseInvstgService caseInvstgService;

    /**
     * 跳转到查询页面
     *
     * @param
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','案件调查-0','案件调查-1')")
    @RequestMapping(value = "/caseinvstglist", method = RequestMethod.GET)
    public String userToSelect(Model model) {
        model.addAttribute("selectTab", "案件调查");
        return "caseinvstg";//新界面
    }

    @RequestMapping("/query")
    @ResponseBody
    public PageVo query(CaseInvstg caseInvstg, PageVo pageVo) {

        caseInvstg.setCreateTimestart(DateUtil.getStartTimeStr(caseInvstg.getCreateTimestart()));//首次咨询
        caseInvstg.setCreateTimeend(DateUtil.getEndTimeStr(caseInvstg.getCreateTimeend()));//末次咨询
        caseInvstg.setFbdatetimestart(DateUtil.getStartTimeStr(caseInvstg.getFbdatetimestart()));//发布日期
        caseInvstg.setFbdatetimeend(DateUtil.getEndTimeStr(caseInvstg.getFbdatetimeend()));//发布日期
        caseInvstg.setLqdatetimestart(DateUtil.getStartTimeStr(caseInvstg.getLqdatetimestart()));//领取日期
        caseInvstg.setLqdatetimeend(DateUtil.getEndTimeStr(caseInvstg.getLqdatetimeend()));//领取日期
        List<CaseInvstg> vos= caseInvstgService.queryCaseInvstg(caseInvstg,pageVo);
        int pageIndex=pageVo.getPageNum();
        int size =pageVo.getPageSize();
        for (int i=1;i<=vos.size();i++
             ) {
            vos.get(i-1).setRowno(i+(pageIndex-1)*size);
        }
        pageVo.setResult(vos);
        return pageVo;
    }

    @PreAuthorize("hasAnyAuthority('ADMIN','案件调查-0','案件调查-1')")
    @RequestMapping("/update")
    @ResponseBody
    public PageVo update(CaseInvstg caseInvstg, PageVo pageVo) {
          caseInvstg.setFbdatetime(DateUtil.getCurrentTimestamp());//发布日期
          caseInvstgService.update(caseInvstg);//更新发布日期 案件等级  备注
          return pageVo;
    }

    @RequestMapping("/count")
    @ResponseBody
    public int count(CaseInvstg caseInvstg, PageVo pageVo) {
        caseInvstg.setCreateTimestart(DateUtil.getStartTimeStr(caseInvstg.getCreateTimestart()));//首次咨询
        caseInvstg.setCreateTimeend(DateUtil.getEndTimeStr(caseInvstg.getCreateTimeend()));//末次咨询
        caseInvstg.setFbdatetimestart(DateUtil.getStartTimeStr(caseInvstg.getFbdatetimestart()));//发布日期
        caseInvstg.setFbdatetimeend(DateUtil.getEndTimeStr(caseInvstg.getFbdatetimeend()));//发布日期
        caseInvstg.setLqdatetimestart(DateUtil.getStartTimeStr(caseInvstg.getLqdatetimestart()));//领取日期
        caseInvstg.setLqdatetimeend(DateUtil.getEndTimeStr(caseInvstg.getLqdatetimeend()));//领取日期
        int total = caseInvstgService.countCaseInvstg(caseInvstg);
        return total;
    }

    @RequestMapping("/xls")
    @ResponseBody
    public void xls(HttpServletResponse response, HttpServletRequest request) {
        CaseInvstg vo = new CaseInvstg();
        vo.setUname(request.getParameter("uname"));//客户名称
        vo.setUphone(request.getParameter("uphone"));//客户电话
        vo.setArea(request.getParameter("area"));//地区
        vo.setCreateTimestart(DateUtil.getStartTimeStr(request.getParameter("createTimestart")));//首次咨询
        vo.setCreateTimeend(DateUtil.getEndTimeStr(request.getParameter("createTimeend")));//末次咨询

        vo.setFbdatetimestart(DateUtil.getStartTimeStr(request.getParameter("fbdatetimestart")));//
        vo.setFbdatetimeend(DateUtil.getEndTimeStr(request.getParameter("fbdatetimeend")));//
        vo.setLqdatetimestart(DateUtil.getStartTimeStr(request.getParameter("lqdatetimestart")));
        vo.setLqdatetimeend(DateUtil.getEndTimeStr(request.getParameter("lqdatetimeend")));

        if(null!=request.getParameter("level")&& ""!=request.getParameter("level")){
            vo.setLevel(Integer.valueOf(request.getParameter("level")));
        }

        if(null!=request.getParameter("buystatus")&& ""!=request.getParameter("buystatus")){
            vo.setBuystatus(Integer.valueOf(request.getParameter("buystatus")));
        }


        try {
            expExcel(request, response, vo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }







    /**
     *导出xls
     * @param request
     * @param resp
     * @param   caseInvstg  查询条件
     * @return
     * @throws IOException
     */
    public   void expExcel(HttpServletRequest request, HttpServletResponse resp, CaseInvstg caseInvstg) throws IOException {


        List<CaseInvstg> vos_call =caseInvstgService.queryCaseInvstg(caseInvstg);
        HSSFWorkbook wb = new HSSFWorkbook();
        request.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/x-download");
        String fileName = "数据分析"+System.currentTimeMillis()+".xls";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        resp.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        String[] headers={ "用户姓名","用户电话", "用户归属地","提交日期","案件类型","案件描述" ,"投票调查" ,"备注","发布时间","领取时间","领取律师ID","案件等级","案件金额" ,"购买方式","交易单号","交易时间","交易账户"};
        HSSFSheet sheet = wb.createSheet("数据分析");
        sheet.setDefaultRowHeight((short) (2 * 256));
        //sheet.setColumnWidth(0, 50 * 160);
        HSSFCellStyle cellStyle = wb.createCellStyle();
        HSSFCellStyle cellStyle_content = wb.createCellStyle();

        HSSFRow row =sheet.createRow(0);
        HSSFFont headerFont = wb.createFont(); // 创建字体样式
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗
        headerFont.setFontName("宋体"); // 设置字体类型
        //headerFont.setColor(HSSFFont.COLOR_RED);
        headerFont.setFontHeightInPoints((short) 14); // 设置字体大小
        cellStyle.setFont(headerFont); // 为标题样式设置字体样式
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中


        for (int i=0;i<headers.length;i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(cellStyle);
        }

        HSSFFont font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 14);
        int rowCount=0;
        if(null!=vos_call&&vos_call.size()>0){
            for (int i = 0; i < vos_call.size(); i++)
            {
                HSSFRow row1 = sheet.createRow((int) i + 1);
                rowCount++;
                //row1.setRowStyle(cellStyle);
                CaseInvstg vo = vos_call.get(i);
                int j=0;
                //row1.createCell(j++).setCellValue(i + 1);
                HSSFCell cell;
                cellStyle_content.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 水平方向的对齐方式
                row1.createCell(j++).setCellValue(String.valueOf(vo.getUname()==null?"":vo.getUname()));//
                row1.createCell(j++).setCellValue(String.valueOf(vo.getUphone()==null?"":vo.getUphone()));//
                row1.createCell(j++).setCellValue(String.valueOf(vo.getArea()==null?"":vo.getArea()));//
                row1.createCell(j++).setCellValue(String.valueOf(vo.getCreateTime()==null?"":DateUtil.TimeStampToString(vo.getCreateTime(), "yyyy-MM-dd HH:mm:ss") ));//用户注册时间
                row1.createCell(j++).setCellValue(String.valueOf(vo.getCasetypename()==null?"":vo.getCasetypename()));//
                row1.createCell(j++).setCellValue(String.valueOf(vo.getCasedesc()==null?"":vo.getCasedesc()));//
                row1.createCell(j++).setCellValue(String.valueOf(vo.getQuestionnaire()==null?"":vo.getQuestionnaire()));//
                row1.createCell(j++).setCellValue(String.valueOf(vo.getRemark()==null?"":vo.getRemark()));//备注
                row1.createCell(j++).setCellValue(String.valueOf(vo.getFbdatetime()==null?"":vo.getFbdatetime()));//
                row1.createCell(j++).setCellValue(String.valueOf(vo.getLqdatetime()==null?"":vo.getLqdatetime()));//
                row1.createCell(j++).setCellValue(String.valueOf(vo.getLvid()==null?"":vo.getLawyername()));//
                row1.createCell(j++).setCellValue(String.valueOf(vo.getLevel()==null?"":vo.getLevel()));//

                //2017-08-16
                row1.createCell(j++).setCellValue(String.valueOf(vo.getAjje()==null?"":vo.getAjje()));//案件金额
                row1.createCell(j++).setCellValue(String.valueOf(vo.getGmfs()==null?"":(vo.getGmfs()==0?"微信":"支付宝")));//购买方式(微信、支付宝)
                row1.createCell(j++).setCellValue(String.valueOf(vo.getJydh()==null?"":vo.getJydh()));//交易单号(律师支付的交易单号)
                row1.createCell(j++).setCellValue(String.valueOf(vo.getJydate()==null?"":vo.getJydate()));//交易时间
                row1.createCell(j++).setCellValue(String.valueOf(vo.getJyzh()==null?"":vo.getJyzh()));//交易账户(对应微信或支付宝账号)
            }
        }

        OutputStream out = resp.getOutputStream();
        wb.write(out);
        out.close();
    }
}
